package com.tiku.handler;

import com.tiku.common.ResultJson;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.*;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    // 处理所有运行时异常
    @ExceptionHandler(RuntimeException.class)

    public ResultJson handleRuntimeException(RuntimeException ex) {

        return ResultJson.error("服务器开小差啦,请稍后再试");
    }


    // 处理其他异常
    @ExceptionHandler(value = {BadCredentialsException.class, InternalAuthenticationServiceException.class})
    public ResultJson handleException(Exception ex) {
        return ResultJson.error(ex.getMessage());

    }

    // 错误响应类
    public static class ErrorResponse {
        private int status;
        private String message;

        public ErrorResponse(int status, String message) {
            this.status = status;
            this.message = message;
        }

        // Getter and Setter
        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}
